package com.mf.basic.services;

import cn.dev33.satoken.stp.StpUtil;
import com.mf.basic.entity.DtoRole;
import com.mf.basic.entity.DtoUserRole;
import com.mf.basic.enums.EnumBaseParams;
import com.mf.basic.exceptions.MyException;
import com.mf.basic.mapper.MapperRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * Created by Luke on 2025/3/8 17:03.
 */
@Service
@Transactional
public class ServiceRole extends ServiceImpl<MapperRole, DtoRole> implements BaseService<DtoRole>{
    @Override
    public LambdaQueryWrapper<DtoRole> fillParams(Map<String, Object> params) {
        LambdaQueryWrapper<DtoRole> qw = loadQw();
        Object tmp = params.get("key");
        if (paramNotNull(tmp)) {
            String k = tmp + "";
            qw.like(DtoRole::getRoleName, k).or()
                    .like(DtoRole::getRemark, k);
        }
        qw.orderByDesc(DtoRole::getId);
        return qw;
    }

    @Resource
    @Lazy
    private ServiceUserRole serviceUserRole;

    /**
     * 创建角色
     * @param roleName 角色名
     * @param remark 备注
     */
    public synchronized void doAdd(String roleName, String remark) {
        DtoRole role = new DtoRole()
                .setId(null)
                .setRoleName(roleName)
                .setRemark(remark)
                .setDouser((String) StpUtil.getSession().get(EnumBaseParams.douser.name()));
        try {
            this.save(role);
        }catch (Exception e){
            throw new MyException("角色名已经存在, 请检查");
        }
    }

    /**
     * 修改角色
     * @param id ID
     * @param roleName 角色名称
     * @param remark 备注
     */
    public synchronized void doUpdate(Long id, String roleName, String remark) {
        //验证角色ID
        DtoRole role = getById(id);
        Assert.state(role != null, "角色("+id+")不存在, 请检查");
        role = new DtoRole()
                .setId(id)
                .setRoleName(roleName)
                .setRemark(remark);
        try {
            this.updateById(role);
        }catch (Exception e){
            throw new MyException("角色名已经存在, 请检查");
        }
    }

    /**
     * 删除角色
     * @param id ID
     * @param roleName 角色名称, 防止非法误删
     */
    public synchronized void doDelete(Long id, String roleName) {
        //验证角色ID
        DtoRole role = getById(id);
        Assert.state(id != 1, "该角色为系统内置, 请勿删除");
        Assert.state(role != null, "角色("+id+")不存在, 请检查");
        Assert.state(role.getRoleName().equals(roleName), "角色名("+roleName+")与角色ID("+id+")不匹配, 请检查");
        //判断角色是否有用户关联
        List<DtoUserRole> tmpList = serviceUserRole.findByRoleId(id);
        Assert.state(tmpList.size() == 0, "角色("+role.getRoleName()+")有用户关联, 请先解除关联");
        //删除角色
        this.removeById(id);
    }

}
